using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Home.Site.Models.DataLayer
{
    public class MusicAlbumsRepository : IMusicAlbumsRepository
    {
        readonly PromikeDBEntities context = new PromikeDBEntities();

        public IQueryable<MusicPlaylist> GetMusicAlbumsByUser(string userName)
        {
            return context.MusicPlaylists.Where(a=>a.author==userName);
        }

        public MusicPlaylist Find(int id)
        {
            return context.MusicPlaylists.Find(id);
        }

        public void InsertOrUpdate(MusicPlaylist musicPlaylist)
        {
            if (musicPlaylist.idplaylist == default(int))
            {
                // New entity
                context.MusicPlaylists.Add(musicPlaylist);
            }
            else
            {
                // Existing entity
                context.Entry(musicPlaylist).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var musicAlbum = context.MusicPlaylists.Find(id);
            context.MusicPlaylists.Remove(musicAlbum);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }
}